#!/bin/bash

# 配置Hadoop集群的主机名
NAMENODE_HOST="hadoop102"
RESOURCEMANAGER_HOST="hadoop103"
DATANODE_HOSTS=("hadoop102" "hadoop103" "hadoop104") # 可以添加更多的DataNode主机

# 配置文件路径
CONFIG_FILE=".hadoop_path"

# 检查配置文件是否存在，如果不存在则提示用户输入Hadoop路径
if [ ! -f "$CONFIG_FILE" ]; then
    read -p "请输入Hadoop的安装路径: " HADOOP_HOME
    echo "$HADOOP_HOME" > "$CONFIG_FILE"
else
    HADOOP_HOME=$(cat "$CONFIG_FILE")
fi

# 检查Hadoop路径是否存在
if [ ! -d "$HADOOP_HOME" ]; then
    echo "错误: 指定的Hadoop路径不存在!"
    exit 1
fi

# 读取slaves文件获取集群机器列表
SLAVES_FILE="$HADOOP_HOME/etc/hadoop/slaves"
if [ ! -f "$SLAVES_FILE" ]; then
    echo "错误: 无法找到slaves文件!"
    exit 1
fi

# 检查slaves文件中的机器数量
MACHINE_COUNT=$(wc -l < "$SLAVES_FILE")
echo "检测到$MACHINE_COUNT台机器在slaves文件中。"

# 检查每台机器的Hadoop配置
check_hadoop_config() {
    for MACHINE in $(cat "$SLAVES_FILE"); do
        if ssh $MACHINE test -d "$HADOOP_HOME"; then
            echo "Hadoop配置在$MACHINE上存在。"
        else
            echo "警告: Hadoop配置在$MACHINE上不存在！"
        fi
    done
}

# 检查主机名是否有效
check_hosts() {
    # 检查NameNode主机
    if ! ssh -o BatchMode=yes -o ConnectTimeout=5 $NAMENODE_HOST true &> /dev/null; then
        echo "错误: NameNode主机 ($NAMENODE_HOST) 无法访问！"
        exit 1
    fi

    # 检查ResourceManager主机
    if ! ssh -o BatchMode=yes -o ConnectTimeout=5 $RESOURCEMANAGER_HOST true &> /dev/null; then
        echo "错误: ResourceManager主机 ($RESOURCEMANAGER_HOST) 无法访问！"
        exit 1
    fi

    # 检查DataNode主机
    for DATANODE in "${DATANODE_HOSTS[@]}"; do
        if ! ssh -o BatchMode=yes -o ConnectTimeout=5 $DATANODE true &> /dev/null; then
            echo "错误: DataNode主机 ($DATANODE) 无法访问！"
            exit 1
        fi
    done
}

# 显示当前时间和用户名
show_info() {
    echo "当前时间：$(date '+%Y-%m-%d %H:%M:%S')"
    echo "用户名：$(whoami)"
}

# 检查Hadoop服务状态
check_services() {
    SERVICES=("NameNode" "DataNode" "SecondaryNameNode" "ResourceManager" "NodeManager")
    for SERVICE in "${SERVICES[@]}"; do
        for MACHINE in $(cat "$SLAVES_FILE"); do
            if ssh $MACHINE "jps | grep -w $SERVICE > /dev/null"; then
                echo "$MACHINE: $SERVICE 正在运行。"
            fi
        done
    done
}

# 检查Hadoop服务是否已启动
check_services_running() {
    local running_services=0
    SERVICES=("NameNode" "DataNode" "SecondaryNameNode" "ResourceManager" "NodeManager")
    for SERVICE in "${SERVICES[@]}"; do
        for MACHINE in $(cat "$SLAVES_FILE"); do
            if ssh $MACHINE "jps | grep -w $SERVICE > /dev/null"; then
                ((running_services++))
            fi
        done
    done
    if [ $running_services -gt 0 ]; then
        return 0
    else
        return 1
    fi
}

# 主菜单
main_menu() {
    while true; do
        clear
        show_info
        echo "====================== Hadoop 集群管理工具 ======================"
        echo "当前时间：$(date '+%Y-%m-%d %H:%M:%S')"
        echo "用户名：$(whoami)"
        echo ""
        check_services
        echo ""
        echo "请选择操作:"
        echo "1. 启动Hadoop集群"
        echo "2. 停止Hadoop集群"
        echo "3. 退出脚本"
        read -p "输入选项 (1-3): " CHOICE
        
        case $CHOICE in
            1) start_hadoop ;;
            2) stop_hadoop ;;
            3) exit 0 ;;
            *) echo "无效的选择，请重新输入。" ;;
        esac
    done
}

# 启动Hadoop服务
start_hadoop() {
    if check_services_running; then
        read -p "Hadoop相关服务已启动，请输入YES后会再次启动: " confirm
        if [ "$confirm" != "YES" ]; then
            echo "取消启动操作。"
            return
        fi
    fi

    echo " =================== 启动 hadoop集群 ==================="
    echo " --------------- 启动 hdfs ---------------"
    ssh $NAMENODE_HOST "$HADOOP_HOME/sbin/start-dfs.sh"
    echo " --------------- 启动 yarn ---------------"
    ssh $RESOURCEMANAGER_HOST "$HADOOP_HOME/sbin/start-yarn.sh"
}

# 停止Hadoop服务
stop_hadoop() {
    echo " =================== 关闭 hadoop集群 ==================="
    echo " --------------- 关闭 yarn ---------------"
    ssh $RESOURCEMANAGER_HOST "$HADOOP_HOME/sbin/stop-yarn.sh"
    echo " --------------- 关闭 hdfs ---------------"
    ssh $NAMENODE_HOST "$HADOOP_HOME/sbin/stop-dfs.sh"
}

# 执行检查Hadoop配置
check_hadoop_config

# 检查主机名是否有效
check_hosts

# 进入主菜单
main_menu